package com.kaibai.service.impl;

import com.kaibai.dao.GoodDao;
import com.kaibai.dao.impl.GoodDaoImpl;
import com.kaibai.pojo.Good;
import com.kaibai.pojo.Page;
import com.kaibai.service.GoodService;
import com.kaibai.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GoodServiceImpl implements GoodService {

    private GoodDao dao = new GoodDaoImpl();

    @Override
    public int insert(Good good) {
        Connection conn = null;
        int i = -1;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            i = dao.insert(conn,good);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                i = -1;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return i;
    }

    @Override
    public int deleteById(int id) {
        Connection conn = null;
        int i = -1;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            i = dao.deleteById(conn,id);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                i = -1;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return i;
    }

    @Override
    public int update(Good good) {
        Connection conn = null;
        int i = -1;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            i = dao.update(conn,good);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                i = -1;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return i;
    }

    @Override
    public Good getGoodById(int id) {
        Connection conn = null;
        Good good = null;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            good = dao.getGoodById(conn,id);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                good = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return good;
    }

    @Override
    public List<Good> getAll() {
        Connection conn = null;
        List<Good> list = null;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            list = dao.getAll(conn);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                list = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return list;
    }

    @Override
    public Page<Good> page(int pageNo, int pageSize) {
        Connection conn = null;
        Page<Good> page = null;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            page = new Page<>();
            page.setPageSize(pageSize);
            Integer pageTotalCount = dao.getForPageTotalCount(conn);
            page.setPageTotalCount(pageTotalCount);
            Integer pageTotal = pageTotalCount / pageSize;
            if(pageTotalCount % pageSize > 0){
                pageTotal++;
            }
            page.setPageTotal(pageTotal);

            page.setPageNo(pageNo);
            int begin = (page.getPageNo() - 1) * pageSize;
            List<Good> list = dao.getForPageItems(conn,begin,pageSize);
            page.setItems(list);
            conn.commit();
        } catch (SQLException throwables) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
                page = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return page;
    }

    @Override
    public Page<Good> pageBySearch(int pageNo, int pageSize, String name, int min, int max) {
        if(name == null || "".equals(name)){
            return pageByPrice(pageNo,pageSize,min,max);
        }
        Connection conn = null;
        Page<Good> page = null;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            page = new Page<>();

            page.setPageSize(pageSize);
            Integer pageTotalCount = dao.getForPageTotalCountBySearch(conn,name,min,max);
            page.setPageTotalCount(pageTotalCount);
            Integer pageTotal = pageTotalCount / pageSize;
            if(pageTotalCount % pageSize > 0){
                pageTotal++;
            }
            page.setPageTotal(pageTotal);

            page.setPageNo(pageNo);
            int begin = (page.getPageNo() - 1) * pageSize;
            List<Good> list = dao.getForPageItemsBySearch(conn,begin,pageSize,name,min,max);
            page.setItems(list);

            conn.commit();
        } catch (SQLException throwables) {
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return page;

    }


    @Override
    public Page<Good> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Connection conn = null;
        Page<Good> page = null;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            page = new Page<>();

            page.setPageSize(pageSize);
            Integer pageTotalCount = dao.getForPageTotalCountByPrice(conn,min,max);
            page.setPageTotalCount(pageTotalCount);
            Integer pageTotal = pageTotalCount / pageSize;
            if(pageTotalCount % pageSize > 0){
                pageTotal++;
            }
            page.setPageTotal(pageTotal);

            page.setPageNo(pageNo);
            int begin = (page.getPageNo() - 1) * pageSize;
            List<Good> list = dao.getForPageItemsByPirce(conn,begin,pageSize,min,max);
            page.setItems(list);

            conn.commit();
        } catch (SQLException throwables) {
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return page;
    }

    @Override
    public Page<Good> pageByName(int pageNo, int pageSize, String name) {
        Connection conn = null;
        Page<Good> page = null;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            page = new Page<>();
            page.setPageSize(pageSize);
            Integer pageTotalCount = dao.getForPageTotalCountByName(conn,name);
            page.setPageTotalCount(pageTotalCount);
            Integer pageTotal = pageTotalCount / pageSize;
            if(pageTotalCount % pageSize > 0){
                pageTotal++;
            }
            page.setPageTotal(pageTotal);

            page.setPageNo(pageNo);
            int begin = (page.getPageNo() - 1) * pageSize;
            List<Good> list = dao.getForPageItemsByName(conn,begin,pageSize,name);
            page.setItems(list);
            conn.commit();
        } catch (SQLException throwables) {
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return page;
    }
}
